<template>
  <view>
    <view class='search-area'>
      <uni-search-bar @confirm="search" @input="input" cancelButton="none" radius="100" :focus="true" v-model="keyword">
      </uni-search-bar>
    </view>
    <view class="history" v-if="searchList.length==0">
      <view class="title">
        <text class="text">历史记录</text>
        <uni-icons type="trash" class='icon' @click="clear"></uni-icons>
      </view>
      <view class="history_item" @click='gotoSearch'>
        <uni-tag :text="item"  v-for="(item,index) in historyList" :key='index' class='tag' :data-keyword='item' :inverted="true"></uni-tag>
      </view>
    </view>
    <view class="main" @click='gotoDetail'>
      <view class="main_item" v-for="item in searchList" :key='item.goods_id'>
        <text class="text">{{item.goods_name}}</text>
        <uni-icons type="arrowright" size="16" class='icon' :data-Kuid='item.goods_id'></uni-icons>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    reqSearchList
  } from '../../api/api.js'
  export default {
    data() {
      return {
        timer: null,
        keyword: '',
        searchList: [],
        historyList:[]
      }
    },
    onLoad() {
      this.historyList=JSON.parse(uni.getStorageSync('keyword')||'[]');
    },
    onUnload() {
      uni.setStorageSync('keyword',JSON.stringify(this.historyList));
    },
    methods: {
      clear(){
        this.historyList=[];
      },
      async getSearchList() {
        if (this.keyword.length == 0) {
          this.searchList = [];
          return;
        }
        let res = await reqSearchList(this.keyword);
        if (res.data.meta.status === 200) {
          this.searchList = res.data.message;
          let index=-1;
          if((index=this.historyList.indexOf(this.keyword))==-1){
            this.historyList.unshift(this.keyword);
          }else{
            this.historyList.splice(index,1);
            this.historyList.unshift(this.keyword);
          }
        } else {
          return uni.$showMsg();
        }
      },
      input(value) {
        if (this.timer) {
          clearTimeout(this.timer);
        }
        this.timer = setTimeout(() => {
          this.getSearchList();
        }, 1000)
      },
      gotoDetail(e){
        
        if(e.target.dataset.kuid){
          uni.navigateTo({
            url:'/subpkg/goods_detail/goods_detail?goods_id='+e.target.dataset.kuid
          })
        }
      },
      gotoSearch(e){
        this.keyword=e.target.dataset.keyword;
      }
    }
  }
</script>

<style lang="scss" scoped>
  .search-area {
    background-color: #e00000;
    position: sticky;
    top: 0;
    z-index: 999;
  }
  .history{
    margin: 0 20rpx;
    .title{
      line-height: 80rpx;
      border-bottom: 2rpx solid #ddd;
      height: 80rpx;
      margin-bottom: 20rpx;
      .text{
        font-size: 13px;
        font-weight: 600;
        letter-spacing:5rpx;
      }
      .icon{
        float: right;
        font-size: 14px;
      }
    }
    .history_item{
      .tag{
        margin-right: 38rpx;
        margin-bottom: 30rpx;
        display: inline-block;
      }
    }
  }
  .main{
    padding: 0 20rpx;
    .main_item{
      height: 90rpx;
      border-bottom: 2rpx solid #ddd;
      line-height: 90rpx;
      .text{
        white-space: nowrap;
        text-overflow: ellipsis;
        display: inline-block;
        overflow: hidden;
        width: 90%;
        font-size: 13px;
        font-weight: bold;
      }
      .icon{
        float: right;
      }
    }
  }
</style>
